import { Component } from '@angular/core';
import { Contacts,ContactFindOptions ,ContactFieldType} from '@ionic-native/contacts';

@Component({
    selector: 'page-addresslist',
    templateUrl: 'addresslist.html'
})
export class AddressListPage {
    tabBarElement: any;
    formatContacts: any=[];
    constructor(private contacts: Contacts) {
        // If we navigated to this page, we will have an item available as a nav param
        if (document.querySelector('.tabbar')) {
            this.tabBarElement = document.querySelector('.tabbar.show-tabbar');
        }

    }

    ionViewWillEnter() {
        if (this.tabBarElement) {
            this.tabBarElement.style.display = 'none';
        }
    }

    ionViewWillLeave() {
        if (this.tabBarElement) {
            this.tabBarElement.style.display = 'flex';
        }
    }

    public getContact() {
        let options = new ContactFindOptions();
        let fields: ContactFieldType[];
        fields = ["displayName", "phoneNumbers"];
        options.filter = "";
        options.multiple = true;
        options.hasPhoneNumber = true;
        this.contacts.find(fields, options).then((result) => {
            this.onSuccess(result);
        });
    }
    public onSuccess(contacts: any) {
        //alert('all contacts==' + JSON.stringify(contacts));
        var contactsLength = contacts.length;
        //alert(JSON.stringify(contacts[1]));
        for (let i = 0; i < contactsLength; i++) {
            let obj: any={};
            obj.displayName = contacts[i]["_objectInstance"].displayName;
            obj.phoneNumber = contacts[i]["_objectInstance"]["phoneNumbers"][0].value;
            this.formatContacts.push(obj);
        }
    }
}